package com.cryrabbit.bmbhome.Service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.baidu.mapapi.model.LatLng;
import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.LocationMode;
import com.baidu.trace.OnEntityListener;
import com.baidu.trace.OnStartTraceListener;
import com.baidu.trace.Trace;

import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by CryRabbit on 2016/7/8.
 */
public class MyService extends Service {
    public  class MyBinder extends Binder{
        public String get(){
            return traces;
        }
    }
    private MyBinder binder=new MyBinder();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    String traces="";
    String today;
    LBSTraceClient client;
    Trace trace;
    LatLng latLng_last;
    OnEntityListener onEntityListener;
    OnStartTraceListener onStartTraceListener;
    int gatherInterval = 3;  //位置采集周期 (s)
    int packInterval = 10;  //打包周期 (s)
    String entityName = "Test3";  // entity标识
    long serviceId = 120356;// 鹰眼服务ID
    int traceType = 2;  //轨迹服务类型

    @Override
    public void onCreate() {
        super.onCreate();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd");
        today=simpleDateFormat.format(new java.util.Date());
        onEntityListener=new OnEntityListener() {
            @Override
            public void onRequestFailedCallback(String s) {
                Log.i("guiji", "entity请求失败的回调接口信息："+s);
            }
            @Override
            public void onQueryEntityListCallback(String arg0) {
                traces=arg0;

            }
        };
        TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        entityName=tm.getSimSerialNumber();
        onStartTraceListener=new OnStartTraceListener() {
            @Override
            public void onTraceCallback(int i, String s) {
                startThread();
            }

            @Override
            public void onTracePushCallback(byte b, String s) {
                Log.i("TAG", "onTracePushCallback=" + s);
            }
        };
        client=new LBSTraceClient(getApplicationContext());
        client.setLocationMode(LocationMode.High_Accuracy);
        client.setProtocolType(0);
        client.setOnEntityListener(onEntityListener);
        client.setOnStartTraceListener(onStartTraceListener);
        trace=new Trace(getApplicationContext(),serviceId,entityName,traceType);
        client.setInterval(gatherInterval,packInterval);
        client.startTrace(trace,onStartTraceListener);
    }
    public void startThread(){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                queryRealtimeTrack();
            }
        },0,10000);
    }
    private void queryRealtimeTrack(){
        String entityName = this.entityName;
        String columnKey = "";
        int returnType = 0;
        int activeTime = 0;
        int pageSize = 10;
        int pageIndex = 1;

        this.client.queryEntityList(
                serviceId,
                entityName,
                columnKey,
                returnType,
                activeTime,
                pageSize,
                pageIndex,
                onEntityListener
        );
    }
}
